package com.tj.exceldemo.excel_demo.handler;

import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.*;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class ProgressHandler extends TextWebSocketHandler {
    private static final Set<WebSocketSession> sessions = new HashSet<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        sessions.add(session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        sessions.remove(session);
    }

    public static void updateProgress(double progress) {
        for (WebSocketSession session : sessions) {
            try {
                session.sendMessage(new TextMessage(String.valueOf(progress)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

